package com.ophiux.cs.api.user.impl;

import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.user.port.IPatientApiPort;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.user.dto.AddUserPatientRequest;
import com.ophiux.cs.application.user.dto.AddUserPatientResponse;
import com.ophiux.cs.application.user.dto.DeleteUserPatientRequest;
import com.ophiux.cs.application.user.dto.DeleteUserPatientResponse;
import com.ophiux.cs.application.user.dto.GetCertificateTypeRequest;
import com.ophiux.cs.application.user.dto.GetCertificateTypeResponse;
import com.ophiux.cs.application.user.dto.GetUserPatientsRequest;
import com.ophiux.cs.application.user.dto.GetUserPatientsResponse;
import com.ophiux.cs.application.user.dto.UpdateUserPatientRequest;
import com.ophiux.cs.application.user.dto.UpdateUserPatientResponse;
import com.ophiux.cs.application.user.service.IAddUserPatientAppService;
import com.ophiux.cs.application.user.service.IDeleteUserPatientAppService;
import com.ophiux.cs.application.user.service.IGetCertificateTypeAppService;
import com.ophiux.cs.application.user.service.IGetUserPatientsAppService;
import com.ophiux.cs.application.user.service.IUpdateUserPatientAppService;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PatientApiService implements IPatientApiPort {
	@Autowired
	IAddUserPatientAppService addUserPatientAppService;
	@Autowired
	IDeleteUserPatientAppService deleteUserPatientAppService;
	@Autowired
	IGetUserPatientsAppService getUserPatientsAppService;
	@Autowired
	IUpdateUserPatientAppService updateUserPatientAppService;
	@Autowired
	IGetCertificateTypeAppService getCertificateTypeAppService;

	@Override
	public String getUserPatients(String data) {
		log.info("getUserPatients接口,请求参数："+data);
		Result<ResponsePage<GetUserPatientsResponse>> result = null;
		Request<GetUserPatientsRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetUserPatientsRequest>>() {
					}.getType();
				}
			});
			
			result=getUserPatientsAppService.getUserPatients(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("getUserPatients接口,返回参数："+returnMessage);
			return returnMessage;
			
		} catch (Exception e) {
			log.info("getUserPatients接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String AddUserPatient(String data) {
		log.info("AddUserPatient接口,请求参数："+data);
		Result<AddUserPatientResponse> result = null;
		Request<AddUserPatientRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<AddUserPatientRequest>>() {
					}.getType();
				}
			});
			
			result=addUserPatientAppService.AddUserPatient(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("AddUserPatient接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("AddUserPatient接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String UpdateUserPatient(String data) {
		log.info("UpdateUserPatient接口,请求参数："+data);
		Result<UpdateUserPatientResponse> result = null;
		Request<com.ophiux.cs.application.user.dto.UpdateUserPatientRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<UpdateUserPatientRequest>>() {
					}.getType();
				}
			});
			
			result=updateUserPatientAppService.UpdateUserPatient(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("UpdateUserPatient接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("UpdateUserPatient接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String deleteUserPatient(String data) {
		log.info("deleteUserPatient接口,请求参数："+data);
		Result<DeleteUserPatientResponse> result = null;
		Request<DeleteUserPatientRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<DeleteUserPatientRequest>>() {
					}.getType();
				}
			});
			
			result=deleteUserPatientAppService.deleteUserPatient(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("deleteUserPatient接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("deleteUserPatient接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String GetCertificateType(String data) {
		log.info("GetCertificateType接口,请求参数："+data);
		Result<List<GetCertificateTypeResponse>> result = null;
		Request<GetCertificateTypeRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetCertificateTypeRequest>>() {
					}.getType();
				}
			});
			
			result=getCertificateTypeAppService.GetCertificateType(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("GetCertificateType接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("GetCertificateType接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

}
